package com.boss.salary.parsingExcel.impl;

import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.dao.IdepartmentInfoDao;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.entity.DepartmentInfo;
import com.boss.salary.vo.AgencySalaryVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("writeSalaryReportExcel")
public class WriteSalaryReportExcel {

    @Autowired
    private IAgencyInfoDao iAgencyInfoDao;

    @Autowired
    private IdepartmentInfoDao idepartmentInfoDao;

    String PATH = "F:\\test";

    public void writeExcel03(List<Map<String, Object>>unitSalaryReport ,List<AgencySalaryVO> columnNameList,OutputStream outputStream) throws Exception{
        AgencyInfo agencyInfo = iAgencyInfoDao.getNameById(String.valueOf(unitSalaryReport.get(0).get("agencyId")));
        List<DepartmentInfo> deptList=idepartmentInfoDao.getAgencyDept(String.valueOf(unitSalaryReport.get(0).get("agencyId")));
        Map<String ,String >mapdept=new HashMap<>();
        if (!CollectionUtils.isEmpty(deptList)) {
            for(DepartmentInfo departmentInfo:deptList){
                mapdept.put(departmentInfo.getId(),departmentInfo.getDeptName());
            }
        }
        //1,创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //Workbook workbook = new XSSFWorkbook();
        //2,创建一个工作表
        Sheet sheet = workbook.createSheet("sheet1");
        //3，创建一行（1，1）
        Row row1 = sheet.createRow(0);
        //4，创建一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("单位");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("单位编码");
        Cell cell13 = row1.createCell(2);
        cell13.setCellValue("科室");
        Cell cell14 = row1.createCell(3);
        cell14.setCellValue("身份证号");
        Cell cell15 = row1.createCell(4);
        cell15.setCellValue("姓名");
        int a=5;
        for (int i = 0; i < columnNameList.size(); i++) {
            row1.createCell(a).setCellValue(columnNameList.get(i).getSalaryName());
            a++;
        }

        for (int i = 0; i < unitSalaryReport.size(); i++) {
            Row row = sheet.createRow(i+1);

            row.createCell(0).setCellValue(agencyInfo.getAgencyName());
            row.createCell(1).setCellValue(agencyInfo.getAgencyCode());
            if(!"null".equals(String.valueOf(unitSalaryReport.get(i).get("deptId")))){
                String deptName =mapdept.get(String.valueOf(unitSalaryReport.get(i).get("deptId")));
                row.createCell(2).setCellValue(deptName);
            }else{
                row.createCell(2).setCellValue("");
            }

            row.createCell(3).setCellValue(String.valueOf(unitSalaryReport.get(i).get("idenNo")));
            row.createCell(4).setCellValue(String.valueOf(unitSalaryReport.get(i).get("personName")));
            for(int j=0; j<columnNameList.size(); j++){
                row.createCell(j+5).setCellValue(String.valueOf(unitSalaryReport.get(i).get(columnNameList.get(j).getColumnName())));
            }
        }

        //FileOutputStream fos = new FileOutputStream(PATH+"03版本Excel测试.xls");
        //生成一张表（IO流）07版本使用xlsx结尾！
        //FileOutputStream fos = new FileOutputStream(PATH+"07版本Excel测试.xlsx");
        //输出
        workbook.write(outputStream);
        //关闭流
        outputStream.close();

        System.out.println("生成成功！");
    }

    public void provinceWriteExcel03(List<AgencySalaryVO> columnNameList,OutputStream outputStream) throws Exception{

        //1,创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //Workbook workbook = new XSSFWorkbook();
        //2,创建一个工作表
        Sheet sheet = workbook.createSheet("sheet1");
        //3，创建一行（1，1）
        Row row1 = sheet.createRow(0);
        //4，创建一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("单位");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("单位编码");
        Cell cell13 = row1.createCell(2);
        cell13.setCellValue("科室");
        Cell cell14 = row1.createCell(3);
        cell14.setCellValue("身份证号");
        Cell cell15 = row1.createCell(4);
        cell15.setCellValue("姓名");
        Cell cell16 = row1.createCell(5);
        cell16.setCellValue("工资发卡银行");
        Cell cell17 = row1.createCell(6);
        cell17.setCellValue("工资卡号");
        Cell cell18 = row1.createCell(7);
        cell18.setCellValue("人员编码");
        Cell cell19 = row1.createCell(8);
        cell19.setCellValue("人员排序");
        Cell cell20 = row1.createCell(9);
        cell20.setCellValue("性别");
        Cell cell21 = row1.createCell(10);
        cell21.setCellValue("工资类型");
        Cell cell22 = row1.createCell(11);
        cell22.setCellValue("工资类型编码");
        Cell cell23 = row1.createCell(12);
        cell23.setCellValue("在职状态");

        int a=13;
        for (int i = 0; i < columnNameList.size(); i++) {
            row1.createCell(a).setCellValue(columnNameList.get(i).getSalaryName());
            a++;
        }

        workbook.write(outputStream);
        //关闭流
        outputStream.close();

        System.out.println("生成成功！");
    }
}
